<?php
if(!defined('BASEPATH'))
	exit('No direct script access allowed');

class Teams extends CI_Controller {

	/**
	 * Index Page for this controller.
	 *
	 * Maps to the following URL
	 * 		http://example.com/index.php/welcome
	 *	- or -
	 * 		http://example.com/index.php/welcome/index
	 *	- or -
	 * Since this controller is set as the default controller in
	 * config/routes.php, it's displayed at http://example.com/
	 *
	 * So any other public methods not prefixed with an underscore will
	 * map to /index.php/welcome/<method_name>
	 * @see http://codeigniter.com/user_guide/general/urls.html
	 */
	public function create() {

		#Input and textarea field attributes
		$data['team_name'] = array('name' => 'team_name', 'id' => 'team_name');
		$data['category'] = array('name' => 'comments', 'id' => 'comments');

		$sql = "SELECT category_id, category_desc, category_sport FROM category;";

		$options = array(0 => "");

		$query = $this -> db -> query($sql);

		//print_r ($query->result());
		//print_r ($options);

		foreach($query->result() as $row) {
			$options[$row -> category_id] = $row -> category_desc . " - " . $row -> category_sport;
			//print_r ($options);
		}

		$sql = "SELECT player_id, player_name FROM players;";

		$players = array(0 => "");

		$query = $this -> db -> query($sql);

		//print_r ($query->result());
		//print_r ($options);

		foreach($query->result() as $row) {
			$players[$row -> player_id] = $row -> player_name;
			//print_r ($options);
		}

		$data['options'] = $options;
		$data['players'] = $players;
		#Checkbox attributes
		$data['team_name'] = array('name' => 'team_name', 'id' => 'team_name');

		#$this -> load -> view('form_view', $data);
		$this -> load -> view('team_new_view', $data);
	}

	public function edit($team_id=FALSE) {

		if($team_id && is_numeric($team_id)) {

			/*
			 team_id
			 team_name
			 logo
			 category
			 captain_id
			 * Array ( [team_name] => Testing [category_id] => 1 [captain_id] => 20 [submit] => Submit )
			 */

			$sql = "SELECT team_id, team_name, category FROM teams where team_id=" . $team_id . ";";

			$query = $this -> db -> query($sql);

			if($query -> num_rows() > 0) {

				$row = $query -> row(0);

				$data['team_id'] = $row -> team_id;
				$data['team_nombre'] = $row -> team_name;
				$data['category'] = $row -> category;

				#Input and textarea field attributes
				$data['team_name'] = array('name' => 'team_name', 'id' => 'team_name', 'value' => $row -> team_name);
				//$data['category'] = array('name' => 'comments', 'id' => 'comments');

				//Pull the categories
				$sql = "SELECT category_id, category_desc, category_sport FROM category;";

				$options = array(0 => "");

				$option_sel = array($data['category']);

				$query = $this -> db -> query($sql);

				foreach($query->result() as $row) {
					$options[$row -> category_id] = $row -> category_desc . " - " . $row -> category_sport;
				}

				/*
				 // Pull the players not in the team.
				 $sql = "SELECT player_id, player_name FROM players where player_id NOT IN(SELECT player_id from team_player_rel where team_id=" . $team_id . ");";

				 $players = array();

				 $query = $this -> db -> query($sql);

				 //print_r($query->result());
				 foreach($query->result() as $row) {
				 array_push($players, array('name' => $row -> player_name, 'id' => $row -> player_id));
				 //$players[$row -> player_id] = $row -> player_name;
				 }

				 // Pull the players in the team.
				 $sql = "SELECT player_id, player_name FROM players WHERE player_id IN(SELECT player_id FROM team_player_rel WHERE team_id=" . $team_id . ");";

				 $playersin = array();

				 $query = $this -> db -> query($sql);

				 //print_r($query->result());
				 foreach($query->result() as $row) {
				 array_push($playersin, array('name' => $row -> player_name, 'id' => $row -> player_id));
				 //$players[$row -> player_id] = $row -> player_name;
				 }

				 $data['playersin'] = $playersin;

				 $data['nonplayers'] = $players;
				 *
				 */
				$data['options'] = $options;
				$data['option_sel'] = $option_sel;

				$data['js'] = 'id="submit-players"';

				$this -> load -> view('team_edit_view', $data);
				//$this -> load -> view('DBL', $data);

			} else {

				$data['action'] = "Editar Equipo";
				$data['message'] = "El Identificador del equipo no fue encontrado en la base de datos.";
				$this -> load -> view('error_view', $data);

			}

		} else {

			$data['action'] = "Editar Equipo";
			$data['message'] = "Identificador del equipo no fue proporcionado o es erroneo.";
			$this -> load -> view('error_view', $data);

		}

	}

	public function submit() {
		/*
		 team_id
		 team_name
		 logo
		 category
		 captain_id
		 * Array ( [team_name] => Testing [category_id] => 1 [captain_id] => 20 [submit] => Submit )
		 */
		$dataQ = array('team_name' => $_POST['team_name'], 'category' => $_POST['category_id']);
		$this -> db -> insert('teams', $dataQ);
		//print_r($_POST);
		$data['url'] = "teams/create";
		$this -> load -> view('success', $data);

	}

	public function save() {
		/*
		 team_id
		 team_name
		 logo
		 category
		 captain_id
		 * Array ( [team_name] => Testing [category_id] => 1 [captain_id] => 20 [submit] => Submit )
		 */

		/*
		 foreach($players as $player) {
		 array_push($data,array('team_id'=>$_POST['team_id'], 'player_id' => $player));
		 }
		 */

		$dataQ = array('team_name' => $_POST['team_name'], 'category' => $_POST['category_id']);

		try {
			$this -> db -> where('team_id', $_POST['team_id']);
			$this -> db -> update('teams', $dataQ);
		} catch (Exception $ex) {
			$data['action'] = "Editar Equipo";
			$data['message'] = $e -> getMessage();
			$this -> load -> view('error_view', $data);
		}

		//$this -> db -> insert('teams', $dataQ);
		print_r($_POST);
		$data['url'] = "teams/edit/" . $_POST['team_id'];
		$this -> load -> view('success', $data);

	}

	public function playerListingAJAX($team_id =FALSE) {

		if($team_id && is_numeric($team_id)) {

			// Pull the players in the team.
			$sql = "SELECT player_id, player_name FROM players WHERE player_id IN(SELECT player_id FROM team_player_rel WHERE team_id=" . $team_id . ");";

			$playersin = array();

			$query = $this -> db -> query($sql);

			//print_r($query->result());
			foreach($query->result() as $row) {
				array_push($playersin, array('name' => $row -> player_name, 'id' => $row -> player_id));
				//$players[$row -> player_id] = $row -> player_name;
			}

			$data['team_id'] = $team_id;
			$data['playersin'] = $playersin;
			$this -> load -> view('player_listing_team', $data);

		} else {

			echo "Error: Identificador de equipo erroneo";

		}
	}

	public function nonPlayerListingAJAX($team_id =FALSE) {

		if($team_id && is_numeric($team_id)) {

			// Pull the players in the team.
			$sql = "SELECT player_id, player_name FROM players WHERE player_id not IN(SELECT player_id FROM team_player_rel WHERE team_id=" . $team_id . ");";

			$playersout = array();

			$query = $this -> db -> query($sql);

			//print_r($query->result());
			foreach($query->result() as $row) {
				array_push($playersout, array('name' => $row -> player_name, 'id' => $row -> player_id));
				//$players[$row -> player_id] = $row -> player_name;
			}

			$data['playersout'] = $playersout;

			$this -> load -> view('non-player_listing_team', $data);

		} else {

			echo "Error: Identificador de equipo erroneo";

		}

	}

	public function saveplayers() {

		// TODO : add condition to avoid repeated players in same category teams.

		$data = array();

		foreach($_POST['team_players'] as &$value) {

			array_push($data, array('team_id' => $_POST['team_id'], 'player_id' => $value));

			//echo "value = ".$value;

		}

		$this -> db -> insert_batch('team_player_rel', $data);
		echo "Operacion completada";

		/*
		 team_id
		 team_name
		 logo
		 category
		 captain_id
		 * Array ( [team_name] => Testing [category_id] => 1 [captain_id] => 20 [submit] => Submit )
		 */

		/*
		 foreach($players as $player) {
		 c
		 }
		 */
		/*
		 $dataQ = array('team_name' => $_POST['team_name'], 'category' => $_POST['category_id']);

		 try {
		 $this -> db -> where('team_id', $_POST['team_id']);
		 $this -> db -> update('teams', $dataQ);
		 $this -> db -> insert_batch('team_player_rel', $data);
		 } catch (Exception $ex) {
		 $data['action'] = "Editar Equipo";
		 $data['message'] = $e -> getMessage();
		 $this -> load -> view('error_view', $data);
		 }

		 //$this -> db -> insert('teams', $dataQ);
		 print_r($_POST);
		 $data['url'] = "teams/edit/" . $_POST['team_id'];
		 $this -> load -> view('success', $data);
		 */
	}

	public function removePlayerFromTeam() {

		//print_r($_POST);

		//$tables = array('table1', 'table2', 'table3');
		$this -> db -> where('team_id', $_POST['team_id']);
		$this -> db -> where('player_id	', $_POST['player_id']);
		$this -> db -> delete('team_player_rel');

		echo "Operation successful";

		/*
		 team_id
		 team_name
		 logo
		 category
		 captain_id
		 * Array ( [team_name] => Testing [category_id] => 1 [captain_id] => 20 [submit] => Submit )
		 */

		/*
		 foreach($players as $player) {
		 array_push($data,array('team_id'=>$_POST['team_id'], 'player_id' => $player));
		 }
		 */
		/*
		 $dataQ = array('team_name' => $_POST['team_name'], 'category' => $_POST['category_id']);

		 try {
		 $this -> db -> where('team_id', $_POST['team_id']);
		 $this -> db -> update('teams', $dataQ);
		 $this -> db -> insert_batch('team_player_rel', $data);
		 } catch (Exception $ex) {
		 $data['action'] = "Editar Equipo";
		 $data['message'] = $e -> getMessage();
		 $this -> load -> view('error_view', $data);
		 }

		 //$this -> db -> insert('teams', $dataQ);
		 print_r($_POST);
		 $data['url'] = "teams/edit/" . $_POST['team_id'];
		 $this -> load -> view('success', $data);
		 */
	}

	public function createTeamCaptain() {

	}

	public function teamlistingAjax($category=FALSE) {

		//echo "in the loop;";
		
		$teamsincat = array();

		if($category) {

			$sql = "SELECT team_id, team_name FROM teams where category = " . $category . ";";

			$query = $this -> db -> query($sql);

			//print_r($query->result());
			foreach($query->result() as $row) {
				array_push($teamsincat, array('team_name' => $row -> team_name, 'team_id' => $row -> team_id));
				//$players[$row -> player_id] = $row -> player_name;
			}

		}

		$data['teams'] = $teamsincat;

		$this -> load -> view('teams_listing_view', $data);

		//echo "Categoria incorrecta.";

	}
	
	public function admon(){
		
		$this -> load -> model('Torneo_Model');
		
		$results = $this -> Torneo_Model -> get_tournaments_active();
		
		$data['tournaments'] = $results;
		
		$this -> load -> view('teams_admon_view', $data);
		
	}
	
	public function tournament($tournament_id){
		
		$this -> load -> model('Torneo_Model');
		
		$teams = $this -> Torneo_Model -> get_teams_on_tournament($tournament_id);
		
		$data['teams'] = $teams;
		
		$this -> load -> view('teams_by_tournament_view', $data);
		
	}

}

/* End of file welcome.php */
/* Location: ./application/controllers/welcome.php */
